package com.five.controller.backstage;

import com.alibaba.fastjson.JSON;
import com.five.dao.backstage.RoleDao;
import com.five.dao.impl.backstage.RoleDaoImple;
import com.five.entity.backstage.TRole;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/role/*")
public class RoleController extends HttpServlet {
    RoleDao roleDao = new RoleDaoImple();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=UTF-8");
        resp.setCharacterEncoding("utf-8");
        //获取一个标识
        String state = req.getParameter("state");
        PrintWriter out = resp.getWriter();
        if (state != null) {
            switch (state) {
                case "doAdd":
                    doAdd(req, resp, out);
                    break;
                case "doUpdate":
                    doUpdate(req, resp, out);
                    break;
                case "doDel":
                    doDel(req, resp, out);
                default:
                    break;
            }
        } else {
            List<Map> list = roleDao.queryAll();
            json(list,out);
        }
        out.flush();
        out.close();
    }

    //删除
    private void doDel(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) throws IOException {
        String id = req.getParameter("id");
        int result = roleDao.delRole(id);
        json(result,out);
    }

    //修改
    private void doUpdate(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) {
        Integer id = Integer.valueOf(req.getParameter("id"));
        String  name= req.getParameter("name");
        TRole tRole = new TRole(id,name);
        int result = roleDao.add(tRole);
        json(result,out);
    }

    //添加
    private void doAdd(HttpServletRequest req, HttpServletResponse resp, PrintWriter out) throws IOException {
        String name = req.getParameter("name");
        TRole tRole = new TRole(name);
        int result = roleDao.add(tRole);
        json(result,out);
    }

    //封装json
    private void json(Object result,PrintWriter out){
        Map map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", 0);
        map.put("data", result);
        String s = JSON.toJSONString(map);
        out.write(s);
    }
}
